package com.movie.ui;

import com.movie.service.AuthService;
import javax.swing.*;
import java.awt.*;

public class LoginUI extends JDialog {

    private JTextField usernameField;
    private JPasswordField passwordField;
    private String userRole;

    public LoginUI(JFrame parent, AuthService authService) {
        super(parent, "登录", true);
        setSize(300, 200);
        setLocationRelativeTo(parent);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setLayout(new GridLayout(3, 2, 10, 10));

        add(new JLabel("Username:"));
        usernameField = new JTextField();
        add(usernameField);

        add(new JLabel("Password:"));
        passwordField = new JPasswordField();
        add(passwordField);

        JButton loginButton = new JButton("Login");
        loginButton.addActionListener(e -> {
            String username = usernameField.getText();
            String password = new String(passwordField.getPassword());
            String role = authService.login(username,password);
            if(role != null){
                userRole = role;
                dispose();
            }else{
                JOptionPane.showMessageDialog(this,"Invalid username or password");
            }
        });

        JButton cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(e -> {
            userRole = null;
            dispose();
        });
        add(loginButton);
        add(cancelButton);

        setVisible(true);
    }
    public String getUserRole() {
        return userRole;
    }
}